package com.winter.service.impl;

import com.winter.mapper.OrderInfoMapper;
import com.winter.mapper.ReplyMapper;
import com.winter.mapper.TopicMapper;
import com.winter.mapper.UserMapper;
import com.winter.model.OrderInfo;
import com.winter.model.Topic;
import com.winter.model.User;
import com.winter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Encoder;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/16.
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper ;//杩欓噷浼氭姤閿欙紝浣嗘槸骞朵笉浼氬奖鍝�
    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private ReplyMapper replyMapper;

    @Override
    public int addUser(User user) {

        return userMapper.insertSelective(user);
    }

    /*
    * 杩欎釜鏂规硶涓敤鍒颁簡鎴戜滑寮�ご閰嶇疆渚濊禆鐨勫垎椤垫彃浠秔agehelper
    * 寰堢畝鍗曪紝鍙渶瑕佸湪service灞備紶鍏ュ弬鏁帮紝鐒跺悗灏嗗弬鏁颁紶閫掔粰涓�釜鎻掍欢鐨勪竴涓潤鎬佹柟娉曞嵆鍙紱
    * pageNum 寮�椤垫暟
    * pageSize 姣忛〉鏄剧ず鐨勬暟鎹潯鏁�
    * */
    @Override
    public List<User> findAllUser(int pageNum, int pageSize) {
        //灏嗗弬鏁颁紶缁欒繖涓柟娉曞氨鍙互瀹炵幇鐗╃悊鍒嗛〉浜嗭紝闈炲父绠�崟銆�
//        PageHelper.startPage(pageNum, pageSize);
        return userMapper.selectAllUser();
    }
    @Override
    public boolean checkMoneyById(Long userId ,BigDecimal money){
        User user=userMapper.selectByPrimaryKey(userId);
        BigDecimal ownMoney =user.getMoney();
        if(ownMoney.compareTo(money)>=0){
             return true;
        }else{
            return false;
        }
    }

    public  User selectByPrimaryKey(Long userId){

        return userMapper.selectByPrimaryKey(userId);
    }

    public int updateByPrimaryKey(User user){
        return userMapper.updateByPrimaryKey(user);

    }

    //撤销
    @Override
    @Transactional
    public void cancelTopic(Map<String, Object> paramMap) {

        //判断是否有中期
        int isMiddle = (int) paramMap.get("isMiddle");

        if (isMiddle == 0) {//有中期
            //如果有中期判断属于哪个状态付款 1中期第一次确认付款  2 中期已经已经验收,枪手打完最后的题目,用户支付尾款 比例2:3:5
            //判断中期订单状态
            long tid = (long) paramMap.get("tid");//获得标题id
            long orderNumber = (long) paramMap.get("orderNumber");//获得订单号

            OrderInfo gunManOrderInfo =orderInfoMapper.selectByTopicId(tid);
            if(gunManOrderInfo.getOrderStatus()==0){ //无人应答
                gunManOrderInfo.setOrderStatus(Byte.parseByte("4"));//订单完结
                gunManOrderInfo.setIsCancel(1);//已撤销
                orderInfoMapper.updateByPrimaryKeySelective(gunManOrderInfo);//撤销订单
                Topic topic =topicMapper.selectByPrimaryKey(tid);
                topic.setFlag(4);
                topic.setIsCancel(1);
                topicMapper.updateByPrimaryKeySelective(topic);//撤销问题
            }else if(gunManOrderInfo.getOrderStatus()==2){//答题中
                System.out.println("枪手已接单,正在答题不能撤销");
                return;

            }else{//中期验收已经通过但是还要撤销
                Topic topic =topicMapper.selectByPrimaryKey(tid);

//                BigDecimal fee = topic.getFee();//提问者支付的赏金
//                BigDecimal gunManFee = topic.getFee().multiply(new BigDecimal(0.3));//中期款
//                long buyId = gunManOrderInfo.getBuyerId();
//                long gunManId = gunManOrderInfo.getGunmanId();
//                //提问者支付中期款
//                User buyer = userMapper.selectByPrimaryKey(buyId);
//                buyer.setMoney(buyer.getMoney().subtract(gunManFee));
//                userMapper.updateByPrimaryKey(buyer);
//                //枪手收中期款
//                User gunMan = userMapper.selectByPrimaryKey(gunManId);
//                buyer.setMoney(buyer.getMoney().add(gunManFee));
//                userMapper.updateByPrimaryKey(gunMan);

                gunManOrderInfo.setOrderStatus(Byte.parseByte("4"));//订单完结
                gunManOrderInfo.setIsCancel(1);//已撤销
                orderInfoMapper.updateByPrimaryKeySelective(gunManOrderInfo);//撤销订单

                topic.setFlag(4);
                topic.setIsCancel(1);
                topicMapper.updateByPrimaryKeySelective(topic);//撤销问题

            }


        } else {//没有中期
            long tid = (long) paramMap.get("tid");//获得标题id
            long orderNumber = (long) paramMap.get("orderNumber");//获得订单号

            Topic topic = topicMapper.selectByPrimaryKey(tid);
            if(topic.getFlag()==1){ //问题状态是无人回答
                topic.setFlag(4);//完结
                topic.setIsCancel(1);//已撤销
                topicMapper.updateByPrimaryKeySelective(topic);
            }

            OrderInfo info =orderInfoMapper.selectByTopicId(tid);
            if(null!=info){
                System.out.println("枪手已接单不能撤销");
                return;
            }

        }

    }

    //MD5加密
    private String Md5(String password){
        try{
            MessageDigest md = MessageDigest.getInstance("MD5");
            // digese---得到byte数组 加密后的
            byte b []= md.digest(password.getBytes());
            //第一种方式：BASE64Encoder (要导依赖包)
            BASE64Encoder bd = new BASE64Encoder();
            //密文1：
            String strpwd = bd.encode(b);
            return strpwd;
        }catch (Exception e){
            e.printStackTrace();
        }

        return  null;
    }
}
